package linda.test;

import java.util.Collection;

import linda.Linda;
import linda.Tuple;

public class BasicTestRMI5 {
    public static final int NBTUPLES = 5;

    public static void main(String[] a) {

        final Linda linda = new linda.server.LindaClient("localhost");
        
        new Thread() {
			public void run() {
				try {
					Thread.sleep(2);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				for (int i = 0; i < NBTUPLES; i++) {
					Tuple t1 = new Tuple(4, 5);
					System.out.println("(" + (i+1) + ") write: " + t1);
					linda.write(t1);
				}
				
				Tuple t2 = new Tuple(Boolean.class, 5);
				System.out.println("write: " + t2);
				linda.write(t2);
				
				Tuple t3 = new Tuple(String.class, 5);
				System.out.println("write: " + t3);
				linda.write(t3);

				linda.debug("(2)");

			}
		}.start();

		new Thread() {
			public void run() {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Tuple template = new Tuple(Integer.class, 5);
				Collection<Tuple> res = linda.takeAll(template);
				System.out.println("(TAKER) Result: got " + res.size() + " tuples");
				for (Tuple t : res) 
					System.out.println("\t" + t);
				
				if (linda.tryTake(template) == null)
					System.out.println("It works: all the tuples have been removed from the memory!");
				else
					System.out.println("It doesn't work: all the tuples should have been removed from the memory!");
				
				if (linda.tryRead(new Tuple(Boolean.class, Integer.class)) != null &&
						linda.tryRead(new Tuple(String.class, Integer.class)) != null)
					System.out.println("It works: others tuples haven't been taken!");
				else
					System.out.println("It doesn't works: others tuples havet been taken!");
				linda.debug("(1)");
			}
		}.start();
		
    }
}
